package zhoujunhao.rbac.view;

import zhoujunhao.rbac.domian.dto.DeptDto;
import zhoujunhao.rbac.domian.vo.DeptVo;
import zhoujunhao.rbac.domian.vo.Result;
import zhoujunhao.rbac.service.IDeptService;
import zhoujunhao.rbac.service.array.DeptService;

public class Deptview extends Baseview<DeptVo, IDeptService> {
    private static final String MENU = """
            ------------
               部门管理
            ------------
            0. 退出
            1. 部门列表
            2. 创建部门
            3. 更新部门
            4. 删除部门
            ------------
            请输入选项：
            """;
    private static Deptview singleton;


    private Deptview() {
        service = DeptService.getInstance();
    }

    public static Deptview getInstance() {
        if (singleton == null) {
            singleton = new Deptview();
        }
        return singleton;
    }

    /**
     * 部门管理
     */
    public void show() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = Mainview.input.nextLine();
            switch (menuChoice) {
                case "0":
                    System.out.println("已返回上一级菜单");
                    break;
                case "1":
                    list();
                    break;
                case "2":
                    create();
                    break;
                case "3":
                    update();
                    break;
                case "4":
                    delete();
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        } while (!"0".equals(menuChoice));

    }


    /**
     * 显示部门管理菜单
     */


    public void list() {
        Result result = service.list();
        cache = (DeptVo[]) result.data;
        System.out.println("ID\t部门列表\t部门电话");
        for (DeptVo dept : cache) {
            System.out.printf("%d\t%s\t%s\n", dept.id, dept.name, dept.phone);
        }
    }

    public void create() {
        System.out.print("请输入部门名称");
        String name = Mainview.input.nextLine();
        System.out.print("请输入部门电话");
        String phone = Mainview.input.nextLine();
        DeptDto dto = new DeptDto(name, phone);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public void update() {
        DeptVo dept = getCacheById();
        if (dept == null) {
            return;
        }
        System.out.print("请输入部门名称，直接按回车表示不修改：");
        String name = Mainview.input.nextLine();
        name = "".equals(name) ? dept.name : name;
        System.out.print("请输入部门电话，直接按回车表示不修改：");
        String phone = Mainview.input.nextLine();
        phone = "".equals(phone) ? dept.phone : phone;


        DeptDto dto = new DeptDto(name, phone);
        Result result = service.update(dto, dept.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();

    }

    public void delete() {
        DeptVo dept = getCacheById();
        if (dept == null) {
            return;
        }
        System.out.print("输入‘y'确认删除，输入其他取消删除");
        String confirm = Mainview.input.nextLine();
        if (!"y".equalsIgnoreCase(confirm)) {
            System.out.println("您已经取消删除");
            return;
        }
        Result result = service.delete(dept.id);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }


}

